package com.jt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.dto.PageParam;
import com.jt.entity.MeterageDict;
import com.jt.entity.MeterageTemperamentReportChildren;
import com.jt.entity.Meterage_ReportChildren;
import com.jt.pojo.*;
import com.jt.service.UserService;
import com.jt.utils.JwtUtil;
import com.jt.vo.RoleVo;
import com.jt.vo.MenuAndRolesVo;
import io.swagger.annotations.ApiResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/user")
public class UserController {
    @Autowired
    public UserService userService;

    @Autowired
    public JwtUtil jwtUtil;

    @Value("${upload.path}")
    private String uploadPath;


    /*登录用户*/
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        User userResult = userService.login(user);
        if (Objects.isNull(userResult)) {
            return Result.error("对象为null");
        }
        System.out.println("登录成功");
        String token = jwtUtil.getToken(userResult.getId());
        return Result.success(token);
    }

    /*获取权限路由菜单*/
    @GetMapping("/getRightUserList")
    public Result getRightUserList() {
        return Result.success(userService.list());
    }


    @GetMapping("/info")
    public Result getInfo(String token) {
        return userService.getInfo(token);
    }
    @PostMapping("/getList")
    public Result getList(@RequestBody PageParam pageParam) {
        return Result.success( userService.getList(pageParam));
    }

    /*保存和修改*/
    @PostMapping("/insert")
    public Result insert(@RequestBody User user) {
        return userService.insertOrUpdate(user);
    }

    /*保存*/
    @GetMapping("/delete")
    public Result delete(String id) {
        return userService.delete(id);
    }



    /*用户分配权限*/
    @PostMapping("/saveRolesByUserId")
    public Result saveRolesByUserId(@RequestBody Map<String, Object>  map) {
        return   userService.saveRolesByUserId(map);
    }

    /*用户删除单个角色*/
    @GetMapping("/deleteRoleIdByUserId")
    public Result deleteRoleIdByUserId(String userId, String roleId) {
        return  userService.deleteRoleIdByUserId(userId, roleId);
    }

    @GetMapping("/exportWps1")
    public void exportWps1(String id, HttpServletResponse response) throws Exception {
        userService.exportWps1(id, response);
    }

    @GetMapping("/export")
    public void exportWps(HttpServletResponse response) throws Exception {
        userService.exportWps(response);
    }


    @GetMapping("/logout")
    public Result logout(HttpServletResponse response) throws Exception {
       return Result.success("退出登录成功");
    }
    @GetMapping("/test")
    public Result test() {
        return Result.success("测试opfeign");
    }

    @PostMapping("/avatar")
    public Result uploadAvatar(@RequestParam("file") MultipartFile file) {
        try {
            // 验证文件类型
            String contentType = file.getContentType();
            if (!contentType.startsWith("image/")) {
             return  Result.error("上传失败");
            }
            // 生成唯一文件名
            String originalFilename = file.getOriginalFilename();
            String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String fileName = UUID.randomUUID().toString() + fileExtension;
            File avatarDir = new File(uploadPath);
            if (!avatarDir.exists()) {
                avatarDir.mkdirs();
            }
            // 保存文件
            File dest = new File(avatarDir, fileName);
            file.transferTo(dest);
            // 返回文件路径
            return Result.success(fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return  Result.error("上传失败");
        }
    }
}


